//
//  UIScrollView+YTParallax.m
//  YiTuUserClient
//
//  Created by 石少庸 on 2017/3/8.
//  Copyright © 2017年 YiTu. All rights reserved.
//

#import "UIScrollView+YTParallax.h"
#import <objc/runtime.h>
#import <YYWebImage/UIImage+YYWebImage.h>
#import <YYWebImage/UIImageView+YYWebImage.h>

#define CoverViewHeight 200

static char UIScrollViewParallaxCover;

@implementation UIScrollView (YTParallax)

#pragma mark - setter

- (void)setParallaxCoverView:(YTScrollParallaxCover *)parallaxCoverView {
    [self willChangeValueForKey:@"parallaxCoverView"];
    objc_setAssociatedObject(self, &UIScrollViewParallaxCover,
                             parallaxCoverView,
                             OBJC_ASSOCIATION_ASSIGN);
    [self didChangeValueForKey:@"parallaxCoverView"];
}

#pragma mark - getter

- (YTScrollParallaxCover *)parallaxCoverView {
    return objc_getAssociatedObject(self, &UIScrollViewParallaxCover);
}

#pragma mark - instence method

- (void)addParallaxCoverWithImage:(UIImage*)image withBlur:(BOOL)blur {
    [self addParallaxCoverWithImage:image imageUrl:nil topView:nil withBlur:blur];
}

- (void)addParallaxCoverWithImage:(UIImage*)image
                      withTopView:(UIView*)topView withBlur:(BOOL)blur {
    [self addParallaxCoverWithImage:image imageUrl:nil topView:topView withBlur:blur];
}

- (void)addParallaxCoverWithImageUrl:(NSString *)imageUrl withBlur:(BOOL)blur {
    [self addParallaxCoverWithImage:nil imageUrl:imageUrl topView:nil withBlur:blur];
}

- (void)addParallaxCoverWithImageUrl:(NSString *)imageUrl
                         withTopView:(UIView *)topView withBlur:(BOOL)blur {
    [self addParallaxCoverWithImage:nil imageUrl:imageUrl topView:topView withBlur:blur];
}

- (void)addParallaxCoverWithImage:(UIImage *)image
                      imageUrl:(NSString *)imageUrl
                          topView:(UIView *)topView
                         withBlur:(BOOL)blur {
    YTScrollParallaxCover *view = [[YTScrollParallaxCover alloc] initWithFrame:CGRectMake(0,0, kScreenWidth, CoverViewHeight) andContentTopView:topView andImageUrl:imageUrl withBlur:blur];
    
    view.backgroundColor = [UIColor clearColor];
    view.scrollView = self;
    
    if (image) {
        view.image = image;
    }
    
    [self addSubview:view];
    
    if (topView) {
        [self addSubview:topView];
    }
    self.parallaxCoverView = view;
}

- (void)removeParallaxCoverView {
    [self.parallaxCoverView removeFromSuperview];
    self.parallaxCoverView = nil;
}

@end

@interface YTScrollParallaxCover ()

@property (strong, nonatomic) NSMutableArray *blurImages;
@property (strong, nonatomic) UIView *topView;
@property (assign, nonatomic) BOOL isBlur;

@end

@implementation YTScrollParallaxCover

- (instancetype)initWithFrame:(CGRect)frame {
    return [self initWithFrame:frame andContentTopView:nil andImageUrl:nil withBlur:NO];
}

- (instancetype)initWithFrame:(CGRect)frame
            andContentTopView:(UIView *)view
                  andImageUrl:(NSString *)imageUrl
                     withBlur:(BOOL)blur {
    self = [super initWithFrame:frame];
    if (self) {
        self.contentMode = UIViewContentModeScaleAspectFill;
        self.clipsToBounds = YES;
        _blurImages = [[NSMutableArray alloc] initWithCapacity:20];
        _topView = view;
        _isBlur = blur;
        if (imageUrl) {
            [self yy_setImageWithURL:[NSURL URLWithString:imageUrl]
                         placeholder:[UIImage imageNamed:@"public_rect_placeImg"]];
        }
    }
    
    return self;
}

- (void)setImage:(UIImage *)image {
    [super setImage:image];
    [_blurImages removeAllObjects];
    [self prepareForBlurImages];
    
}

- (void)prepareForBlurImages {
    UIImage *image = _isBlur ? [self.image yy_imageByBlurLight] : self.image;
    for (NSUInteger i = 0; i < 21; i++) {
        [_blurImages addObject:image];
    }
}

- (void)setScrollView:(UIScrollView *)scrollView {
    [_scrollView removeObserver:self forKeyPath:@"contentOffset"];
    _scrollView = scrollView;
    [_scrollView addObserver:self
                  forKeyPath:@"contentOffset"
                     options:NSKeyValueObservingOptionNew
                     context:nil];
}

- (void)removeFromSuperview {
    [_scrollView removeObserver:self forKeyPath:@"contentOffset"];
    [_topView removeFromSuperview];
    [super removeFromSuperview];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    if (self.scrollView.contentOffset.y < 0) {
        
        CGFloat offset = -self.scrollView.contentOffset.y;
        _topView.frame = CGRectMake(0, -offset, kScreenWidth, _topView.bounds.size.height);
        
        self.frame = CGRectMake(-offset, -offset + _topView.bounds.size.height, kScreenWidth + offset * 2, CoverViewHeight + offset);
        NSInteger index = offset / 10;
        if (index < 0) {
            index = 0;
        }
        else if(index >= _blurImages.count) {
            index = _blurImages.count - 1;
        }
        UIImage *image = _blurImages[index];
        if (self.image != image) {
            [super setImage:image];
        }
    } else {
        _topView.frame = CGRectMake(0, 0, kScreenWidth, _topView.bounds.size.height);
        
        self.frame = CGRectMake(0, _topView.bounds.size.height, kScreenWidth, CoverViewHeight);
        UIImage *image = _blurImages[0];
        
        if (self.image != image) {
            [super setImage:image];
        }
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context {
    [self setNeedsLayout];
}

@end
